import doctest
import unittest

import uptimemonitor as up

from datetime import datetime

sample3 = ' 00:01:04 up 28 min,  2 users,  load average: 0.00, 0.00, 0.07'

class LineParserTestCase(unittest.TestCase):
    def setUp(self):
        self.date = datetime(2008, 7, 9)
        self.output = ' 12:40:02 up 31 days, 15:10,  2 users,  load average: 6.23, 6.02, 6.52'
        
    def test_standard(self):
        lp = up.LineParser(self.output, self.date)
        d = lp.datetime
        self.assertEqual(2008, d.year)
        self.assertEqual(7, d.month)
        self.assertEqual(9, d.day)
        self.assertEqual(12, d.hour)
        self.assertEqual(40, d.minute)
        self.assertEqual(2, d.second)
        self.assertEqual(((31 * 24) + 15) * 60 + 10.0, lp.up_minutes)
        self.assertEqual(2, lp.users)
        self.assertEqual(6.23, lp.load1)
        self.assertEqual(6.02, lp.load5)
        self.assertEqual(6.52, lp.load15)  
        
    def test_uptime(self):
        lp = up.LineParser(self.output, self.date)
        u = lp.uptime
        self.assertTrue(u is not None)
        d = u.time
        self.assertEqual(2008, d.year)
        self.assertEqual(7, d.month)
        self.assertEqual(9, d.day)
        self.assertEqual(12, d.hour)
        self.assertEqual(40, d.minute)
        self.assertEqual(2, d.second)
        self.assertEqual(((31 * 24) + 15) * 60 + 10.0, u.up_minutes)
        self.assertEqual(2, u.users)
        self.assertEqual(6.23, u.load1)
        self.assertEqual(6.02, u.load5)
        self.assertEqual(6.52, u.load15)  
    
    def test_minutes_singleuser(self):
        line = ' 23:35:39 up 2 min,  1 user,  load average: 1.15, 0.75, 0.30'
        lp = up.LineParser(line, self.date)
        d = lp.datetime
        self.assertEqual(2008, d.year)
        self.assertEqual(7, d.month)
        self.assertEqual(9, d.day)
        self.assertEqual(23, d.hour)
        self.assertEqual(35, d.minute)
        self.assertEqual(39, d.second)
        self.assertEqual(2.0, lp.up_minutes)
        self.assertEqual(1, lp.users)
        self.assertEqual(1.15, lp.load1)
        self.assertEqual(0.75, lp.load5)
        self.assertEqual(0.30, lp.load15)  
        
    def test_minutes(self): 
        line = ' 00:01:04 up 28 min,  2 users,  load average: 0.00, 0.00, 0.07'
        lp = up.LineParser(line, self.date)
        d = lp.datetime
        self.assertEqual(2008, d.year)
        self.assertEqual(7, d.month)
        self.assertEqual(9, d.day)
        self.assertEqual(0, d.hour)
        self.assertEqual(1, d.minute)
        self.assertEqual(4, d.second)
        self.assertEqual(28.0, lp.up_minutes)
        self.assertEqual(2, lp.users)
        self.assertEqual(0.0, lp.load1)
        self.assertEqual(0.0, lp.load5)
        self.assertEqual(0.07, lp.load15)  
        
class FileParserTestCase(unittest.TestCase):
    def setUp(self):
        self.date = datetime(2008, 7, 9)
        self.output = ' 12:40:02 up 31 days, 15:10,  2 users,  load average: 6.23, 6.02, 6.52'
        
    def test_standard(self):
        lp = up.LineParser(self.output, self.date)
        d = lp.datetime        
        
def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(LineParserTestCase))
    suite.addTest(doctest.DocTestSuite(up))
    return suite

if __name__ == '__main__':
    unittest.main(defaultTest='suite')